const loadJSScript = scriptUrl => {
  return new Promise((resolve, reject) => {
    if (!scriptUrl) {
      resolve({ code: 404 });
    }
    const oScript = document.createElement('script');
    oScript.type = 'text/javascript';
    oScript.src = scriptUrl;

    // 判断IE8及以下浏览器
    const isIE = !-[1];
    if (isIE) {
      oScript.onreadystatechange = function() {
      // 判断ie 即使404readyState也是loaded或complete
        if (this.readyState === 'loaded' || this.readyState === 'complete') {
          resolve({ code: 200 });
        }
      };
    } else {
      // 判断IE9及以上浏览器，Firefox，Chrome，Opera
      oScript.onload = function() {
        resolve({ code: 200 });
      };
      oScript.onerror = function() {
        resolve({ code: 500 });
      };
    }
    const head = document.getElementsByTagName('head')[0];
    head.appendChild(oScript);
  });
};

export const loadThirdJsFiles = () => {
  window._adoPremise.getCoalThirdJsKeyByAPIPromise = window._UIDesingerAxios({
    method: 'get',
    url: '/component/file/store/address',
  }).then(res => {
    const { data } = res.data || {};
    Object.keys(data || {}).map(key => {
      window._adoPremise[`${key}_loadJSScriptPromise`] = loadJSScript(data[key]);
    });
  });
};